package com.mall.user.services;

import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IUserService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.converter.MemberConverter;
import com.mall.user.converter.UserConverterMapper;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dto.*;
import com.mall.user.utils.AESUtil;
import com.mall.user.utils.JwtTokenUtils;
import com.mall.user.utils.VerifyCodeUtils;
import io.netty.handler.codec.memcache.MemcacheContent;
import org.apache.dubbo.config.annotation.Service;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

@Service
public class IUserServiceImpl implements IUserService {

    @Autowired
    RedissonClient client;

    @Autowired
    KaptchaServiceImpl service;

    @Autowired
    MemberMapper userMapper;

    @Autowired
    UserConverterMapper converter;

    /***
     *
     * @return : 验证失败返回null
     */
    @Override
    public UserLoginResponse userLogin(UserLoginRequest request, String uuid) {

        KaptchaCodeRequest validRequest = new KaptchaCodeRequest();
        validRequest.setCode(request.getCaptcha());
        validRequest.setUuid(uuid);

        UserLoginResponse response = new UserLoginResponse();
        KaptchaCodeResponse kaptchaCodeResponse = service.validateKaptchaCode(validRequest);
        // 验证登录验证码是否正确，不正确返回
        if (kaptchaCodeResponse.getCode().equals(SysRetCodeConstants.KAPTCHA_CODE_ERROR.getCode())) {
            response.setCode(kaptchaCodeResponse.getCode());
            response.setMsg(kaptchaCodeResponse.getMsg());
            return response;
        }

        String userName = request.getUserName();
        String userPwd = new AESUtil(request.getUserPwd()).encrypt();


        // 验证用户名和密码
        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("username", userName).andEqualTo("password", userPwd);
        Member member = userMapper.selectOneByExample(example);
        if (member == null) {
            response.setCode(SysRetCodeConstants.USERORPASSWORD_ERRROR.getCode());
            response.setMsg(SysRetCodeConstants.USERORPASSWORD_ERRROR.getMessage());
            return response;
        }

        String token = JwtTokenUtils.builder().msg(userName).build().creatJwtToken();
        response = this.converter.converter(member);
        response.setToken(token);
        response.setCode("200");
        response.setMsg("登录成功");

        return response;
    }

    @Override
    public CheckAuthResponse tokenValidate(CheckAuthRequest request) {
        String username = JwtTokenUtils.builder().token(request.getToken()).build().freeJwt();
        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("username", username);
        Member member = userMapper.selectOneByExample(example);

        if (member == null) {
            return null;
        }
        return new CheckAuthResponse();
    }


}
